﻿from PySide2.QtCore import *
from PySide2.QtWidgets import *
from PySide2.QtUiTools import *
from main_app_ui import *
import tempfile

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    app.setQuitOnLastWindowClosed(False)

    semaphore = QSystemSemaphore("single app semaphore", 1)
    semaphore.acquire()
    tempdir = tempfile.gettempdir();
    file = QFile(tempdir + "/CuteEdit.lock")
    if file.exists():
        if not file.remove():
            print("lock file is locked.")
            semaphore.release()
            sys.exit(1)

    semaphore.release()

    if file.open(QIODevice.WriteOnly):
        #print("first opened")
        pass
    else:
        print("open acquire lock file failed.")
        sys.exit(2)

    utf8codec = QTextCodec.codecForName("utf-8")
    QTextCodec.setCodecForLocale(utf8codec)
    mainAppUi = MainAppUI()
    if len(sys.argv) > 1:
        mainAppUi.openCommandFiles(sys.argv[1:])
    ret = app.exec_()

    file.close()
    #print("remove lock file:" + file.fileName())
    file.remove()
    sys.exit(ret)
